package cn.tedu.properties.controller;

import cn.tedu.properties.pojo.dto.PropertiesSaveParam;
import cn.tedu.properties.pojo.entity.Property;
import cn.tedu.properties.pojo.param.QueryParam;
import cn.tedu.properties.pojo.vo.PropertyVO;
import cn.tedu.properties.service.PropertiesService;
import cn.tedu.response.ResponseResult;
import cn.tedu.response.StatusCode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "资产管理模块")
@Validated
@RequestMapping("/v1/properties")
public class PropertiesController {
    @Autowired
    private PropertiesService propertiesService;

    @GetMapping("/selectAll")
    @ApiOperation(value = "资产查询功能")
    public ResponseResult selectAllProperties(QueryParam queryParam) {
        log.debug("查询资产入参queryParam={}", queryParam);
        List<PropertyVO> list = propertiesService.selectAll(queryParam);
        log.debug("查询资产入参queryParam={},出参list={}", queryParam, list);
        return ResponseResult.ok(list);
    }

//    @GetMapping("/selectPaging")
//    @ApiOperation(value = "资产表分页查询功能")
//    public ResponseResult selectPaging(QueryParam queryParam) {
//        log.debug("分页查询资产入参:queryParam={}", queryParam);
//        IPage<Property> propertyPage = propertiesService.selectPaging(queryParam);
//        log.debug("分页查询资产入参:{},出参:propertyVOIPage={}", queryParam,propertyPage);
//        return ResponseResult.ok(propertyPage);
//    }
    @GetMapping("/selectPaging")
    @ApiOperation(value = "资产表分页查询功能")
    public ResponseResult selectPaging(QueryParam queryParam) {
        log.debug("分页查询资产入参:queryParam={}", queryParam);
        IPage<PropertyVO> propertyPage = propertiesService.selectPaging(queryParam);
        log.debug("分页查询资产入参:{},出参:propertyVOIPage={}", queryParam,propertyPage);
        return ResponseResult.ok(propertyPage);
    }

    @PostMapping("/create")
    @ApiOperation(value = "新增资产功能")
    public ResponseResult addProperties(@Validated PropertiesSaveParam propertiesSaveParam) {//validation校验
        log.debug("新增资产入参propertiesDTO={}", propertiesSaveParam);
        propertiesService.addProperties(propertiesSaveParam);
        log.debug("新增资产已调用,propertiesDTO={}", propertiesSaveParam);
        return ResponseResult.ok(StatusCode.OPERATION_SUCCESS);
    }

    @PostMapping("/deleteByIds")
    @ApiOperation(value = "资产批量删除功能")
    public ResponseResult deletePropertiesByIds(@NotNull @RequestBody Long[] ids) {//validation校验对象是否为空
        log.debug("删除资产入参propertiesDTO={}", ids);
        propertiesService.deletePropertiesByIds(ids);
        log.debug("删除资产已调用,propertiesDTO={}", ids);
        return ResponseResult.ok();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "资产编辑功能")
    public ResponseResult editProperties(PropertiesSaveParam propertiesSaveParam) {
        log.debug("更新资产信息入参propertiesSaveParam={}", propertiesSaveParam);
        propertiesService.editProperties(propertiesSaveParam);
        log.debug("更新资产信息已调用,propertiesSaveParam={}", propertiesSaveParam);
        return ResponseResult.ok();
    }

    @PostMapping("/scrap")
    @ApiOperation(value = "资产报废功能")
    public ResponseResult scrapProperties(Long id) {
        log.debug("资产报废入参id={}", id);
        propertiesService.scrapProperties(id);
        log.debug("资产报废已调用,id={}", id);
        return ResponseResult.ok();
    }
    @PostMapping("/scrapByIds")
    @ApiOperation(value = "资产批量报废功能")
    public ResponseResult scrapPropertiesByIds(@NotNull @RequestBody Long[] ids) {
        log.debug("资产报废入参ids[]={}", ids.toString());
        propertiesService.scrapPropertiesByIds(ids);
        log.debug("资产报废已调用,ids[]={}", ids);
        return ResponseResult.ok();
    }
}
